package com.progroup.kyrsmoney.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.SQLException;
import android.preference.PreferenceManager;
import android.util.Log;

import com.progroup.checks.Checks;
import com.progroup.kyrsmoney.database.DBAdapter;
import com.progroup.kyrsmoney.parsing.ParseXml;
import com.progroup.kyrsmoney.preferences.PreferencesActivity;

public class AlarmServiceExemple extends BroadcastReceiver {
	 private Context context;
		
	 int i = 0;
	 @Override
	 public void onReceive(Context c, Intent intent) {
		 this.context = c;
		 Log.v(this.getClass().getName(), "Timed alarm onReceive() started at time: " + new java.sql.Timestamp(System.currentTimeMillis()).toString());
		  i=0;
		  int hours = Integer.parseInt(Checks.getTodayHours());
		  
			  if(Checks.getDayOfWeek()!=7&Checks.getDayOfWeek()!=1){
				  if(hours>=9&hours<16){
					  service();
				  }  
			  }
		 
	 }
	 
		  
		  
		  public void service(){
			
				  if(Checks.isOnline(context)){
					  ParseSite();
					  
				  }else{
					  thread_parserUSER();
				  }
		  } 
		  
		  
		  
		  public void thread_parserUSER(){
			  
			 final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
			  new Thread(){
				  public void run() {
					  while(true){
						  try {
							  i++;
							  if(i==4){
								  break;
							  }
							  if(Checks.isOnline(context)){
								  try{
								  ParseSite();
								  }catch (Exception e) {
									// TODO: handle exception
								}
								  break;
								  
							  }
							  if(pref.getBoolean(PreferencesActivity.SAVING_BATTERY, false)){
							    sleep(600000);
							  }else{
								sleep(150000);
							  }
							  Log.i("THREaD111", i+"");
						  } catch (InterruptedException e) {
							  // TODO Auto-generated catch block
							  
							 // e.printStackTrace();
						  }
						  //TODO
					  }
				  };
			  }.start();
		  }
		  
		 
		  
		  public void DeleteRow(){
			  DBAdapter db = new DBAdapter(context);
			  try{
				  int max = DateMax();
				  if(max!=0){
					 
					  db.open();
					  int i =db.Delete(max);
					  Log.i("DELETE_ROW", i+"");
					  db.close();
				  }
			  }catch (SQLException e) {
					// TODO: handle exception
				  db.close();
				}catch (Exception e) {
					// TODO: handle exception
					 db.close();
				}
		  }
		  
		  
		  public int DateMax(){
			  DBAdapter db = new DBAdapter(context);
			  Cursor cursor = db.open().SQL("SELECT date FROM tblBankKyrs WHERE bank_id='1'");
			  int [] date = new int[cursor.getCount()];
			  SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
			  int i = 0;
			  db.close();
			  
			  if (cursor != null||cursor.getCount()!=0) {
				  cursor.moveToFirst();
				  do{
					  date[i]= Integer.parseInt(cursor.getString(0));
					  i++;
				  }while(cursor.moveToNext());
				  cursor.close();
				  
				  if(date.length>pref.getInt(PreferencesActivity.STATISTIC, 10)){
					  return date[date.length-pref.getInt(PreferencesActivity.STATISTIC, 10)];
				  }else{
					  return 0;
				  }
			  }
			  cursor.close();
			return 0;
		  }
		  
		  
		  
		 
		  
		  public int[] getID(){
				DBAdapter db = new DBAdapter(context);
				int [] ID =null;
				try{
				db.open();
				Cursor cursor = db.DB_GET_ID();
				int length = cursor.getCount();
				ID = new int[length];
				if(cursor!=null){
					cursor.moveToFirst();
					int i = 0;
					do{
						ID[i]=cursor.getInt(0);
					    i++;
					}while(cursor.moveToNext());
				}
				cursor.close();
				db.close();
				}catch (Exception e) {
					// TODO: handle exception
					db.close();
				}
				return ID;
			}
			
		  
		  public void ParseSite(){
			  
			 
			
			  ParseXml parser = new ParseXml(context);
			  
			  try{
				  parser.Parsing(getID());
			  }catch (SQLException e) {
				  // TODO: handle exception
			  }catch (Exception e) {
				  // TODO: handle exception
			  }
			  
			  try{
				  DeleteRow();
			  }catch (SQLException e) {
				  // TODO: handle exception
			  }catch (Exception e) {
				  // TODO: handle exception
			  }
			  
			  
			  
		  }
		  
		 
		  
		}